﻿using System;
using Enyim.Caching.Memcached;
using TY.UC.Common.Extension;

namespace TY.UT.Cache.MemCache
{
    /// <summary>
    /// Cached操作
    /// </summary>
    public class MemCachedHelper
    {
 
        private static readonly AspNetCacheConfig aspCache = new AspNetCacheConfig();
        private static readonly MemCached memCache = new MemCached();
        private static readonly string RunEnv = "RunEnv".GetStrValue(); //Product表示正式环境
        private static ICache CacheInstance
        {
            get { if (RunEnv == "Asp") return aspCache; else return memCache; }
        }
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="pKey"></param>
        /// <returns></returns>
        public static object GetObject(string pKey)
        {
            return CacheInstance.GetObject(pKey);
        }


        /// <summary>
        /// 将数据写入缓存，之后需要完善，加密压缩之类的，然后在客户端进行解密也解压，提高安全性与效率。
        /// </summary>
        /// <param name="pKey"></param>
        /// <param name="pValue">
        ///     Value值：请自行将数据源转换成对应的类型，json之类的
        ///     缓存过期于此日期
        /// </param>
        /// <param name="pExpriesAt">默认60分钟</param>
        /// <returns></returns>
        public static bool SetObject(string pKey, object pValue, int pExpriesAt = 60)
        {
            return CacheInstance.SetObject(pKey, pValue, pExpriesAt);
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="pKey"></param>
        /// <returns></returns>
        public static bool RemoveObject(string pKey)
        {
           return CacheInstance.RemoveObject(pKey);
        }
    }
}
